package com.example.test_application;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ActiviteNiveau extends Activity {

	public final static String LEVEL = "level_path";
	public final static String GRID_SIZE = "grid_size";
	public static final String USER_INFO = "UserInfoFile";
	public static final String MAX_UNLOCK = "max_unlock_grid_";

	private int gridSize_ = 0;
	private	ImageView image = null;
	private Button boutonNiveau1 = null;
	private Button boutonNiveau2 = null;
	private Button boutonNiveau3 = null;

	// TODO : faire une methode pour avoir le bon nom de fichier selon le niveau

	private View.OnClickListener clickListenerBoutons = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent secondeActivite = new Intent(ActiviteNiveau.this, GridActivity.class);
			secondeActivite.putExtra(GRID_SIZE, gridSize_);

			switch(v.getId())
			{
			case R.id.button1: // bouton Niveau 1
				secondeActivite.putExtra(LEVEL, 1);
				break;
			case R.id.button2: // bouton Niveau 2
				secondeActivite.putExtra(LEVEL, 2);
				break;
			case R.id.button3: // bouton Niveau 3
				secondeActivite.putExtra(LEVEL, 3); 
				break;
			case R.id.imageView2: // Image Retour
				secondeActivite.setClass(ActiviteNiveau.this, MainActivite.class);
				break;
			}

			startActivity(secondeActivite);
	        finish();
		}
	};

	@SuppressLint("DefaultLocale")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activite_niveau7);

		Intent intent = getIntent();
		gridSize_ = intent.getIntExtra(MainActivite.GRID_SIZE, 0);
		gridSize_ = intent.getIntExtra(GridActivity.GRID_SIZE, 0);

		String title = String.format("Choix du niveau - (%dx%d)", gridSize_, gridSize_);
		setTitle(title);
		
		// On cherche l'image de Retour et on l'assigne au OnClickListener
		image = (ImageView) findViewById(R.id.imageView2);
		image.setOnClickListener(clickListenerBoutons);
		
		// On cherche les boutons et on les assigne au OnClickListener
		boutonNiveau1 = (Button) findViewById(R.id.button1);
		boutonNiveau1.setOnClickListener(clickListenerBoutons);

		boutonNiveau2 = (Button) findViewById(R.id.button2);
		boutonNiveau2.setOnClickListener(clickListenerBoutons);

		boutonNiveau3 = (Button) findViewById(R.id.button3);
		boutonNiveau3.setOnClickListener(clickListenerBoutons);

		setButtonState();

	}

	@Override
	protected void onResume() {
		setButtonState();
		super.onResume();
	}

	public void setButtonState()
	{
		SharedPreferences prefs = getSharedPreferences(USER_INFO, 0);
		int maxUnlock = prefs.getInt(MAX_UNLOCK + gridSize_, 0);

		boutonNiveau2.setEnabled(maxUnlock > 0);

		boutonNiveau3.setEnabled(maxUnlock > 1);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_activite_niveau7, menu);
		return true;
	}

}
